/**
 * 
 */
package client;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 * @author Balduin Landolt
 *
 */
public class HighScoreGUI extends JFrame implements WindowListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Client client;

	/**
	 * @param client 
	 * @param scoreList
	 */
	public HighScoreGUI(Client client, Object[][] scoreList) {
		super("FleetWars! - Highscore");
		this.client=client;
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(this);
		setVisible(true);
		this.setPreferredSize(new Dimension(400, 200));
		this.setResizable(false);
		
		String[] cols=new String[]{"Rang","Name","Punkte"};
		Object[][] sortedList=sort(scoreList);
		
		JTable table=new JTable(sortedList, cols);
		table.getColumnModel().getColumn(0).setPreferredWidth(40);
		table.getColumnModel().getColumn(1).setPreferredWidth(240);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);
		
		add(new JScrollPane(table));
		pack();
		center();
	}
	
	/**
	 * @param scoreList
	 * @return
	 */
	private Object[][] sort(Object[][] scoreList) {
		Object[][] res= new Object[scoreList.length][scoreList[0].length];
		ArrayList<ScoreSatz> list=new ArrayList<HighScoreGUI.ScoreSatz>();
		ArrayList<ScoreSatz> list_sorted=new ArrayList<HighScoreGUI.ScoreSatz>();
		
		for(int i=0;i<scoreList.length;i++){
			int id=Integer.parseInt((String)scoreList[i][0]);
			String nick=(String)scoreList[i][1];
			int score=Integer.parseInt((String)scoreList[i][2]);
			list.add(new ScoreSatz(id, nick, score));
		}
		
		for (ScoreSatz s: list){
			int i=0;
			while(list_sorted.size()>i && list_sorted.get(i)!=null && list_sorted.get(i).compareTo(s)>0)
				i++;
			list_sorted.add(i, s);
		}
		
		for(int i=0;i<res.length;i++){
			res[i][1]=list_sorted.get(i).nick;
			res[i][2]=list_sorted.get(i).score;
			if (i>=1 && res[i][2]==res[i-1][2])
				res[i][0]=res[i-1][0];
			else
				res[i][0]=i+1;
		}
		return res;
	}

	/**
	 * zentriert das GUI am bildschirm
	 */
	private void center() {
		Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
		d.width/=2;
		d.height/=2;
		d.width-=(this.getWidth()/2);
		d.height-=(this.getHeight()/2);
		Point p=new Point(d.width,d.height);
		this.setLocation(p);
	}
	
	public void windowClosing(WindowEvent e) {
		client.highScoreGUIClosed();
		setVisible(false);
		dispose();
	}

	public void windowActivated(WindowEvent arg0) {}
	public void windowClosed(WindowEvent arg0) {}
	public void windowDeactivated(WindowEvent arg0) {}
	public void windowDeiconified(WindowEvent arg0) {}
	public void windowIconified(WindowEvent arg0) {}
	public void windowOpened(WindowEvent arg0) {}
	
	private class ScoreSatz implements Comparable<ScoreSatz>{
		private int score;
		//private int id;
		private String nick;
		
		public ScoreSatz(int id,String nick,int score){
			//this.id=id;
			this.nick=nick;
			this.score=score;
		}
		
		public int compareTo(ScoreSatz o) {
			if (score==o.score)
				return 0;
			else if (score<o.score)
				return -1;
			else
				return 1;
		}
		
	}
}
